package com.lvy.hczn.front.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.core.domain.entity.SysUser;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.common.utils.DictUtils;
import com.lvy.hczn.front.system.domain.system.SysUserBase;
import com.lvy.hczn.front.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lvy.hczn.front.system.mapper.SysUserBaseMapper;
import com.lvy.hczn.front.system.service.ISysUserBaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-24
 */
@Slf4j
@Service
public class SysUserBaseServiceImpl extends ServiceImpl<SysUserBaseMapper, SysUserBase> implements ISysUserBaseService {
    @Autowired
    private SysUserBaseMapper sysUserBaseMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询用户基本信息
     *
     * @param id 用户基本信息主键
     * @return 用户基本信息
     */
    @Override
    public SysUserBase selectSysUserBaseById(String id) {
        return sysUserBaseMapper.selectSysUserBaseById(id);
    }

    /**
     * 查询用户基本信息列表
     *
     * @param sysUserBase 用户基本信息
     * @return 用户基本信息
     */
    @Override
    public List<SysUserBase> selectSysUserBaseList(SysUserBase sysUserBase) {
        return sysUserBaseMapper.selectSysUserBaseList(sysUserBase);
    }

    /**
     * 新增用户基本信息
     *
     * @param sysUserBase 用户基本信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysUserBase(SysUserBase sysUserBase) {
        sysUserBaseMapper.insert(sysUserBase);
        boxSupplierSync(sysUserBase, false);
        return 1;
    }

    /**
     * 修改用户基本信息
     *
     * @param sysUserBase 用户基本信息
     * @return 结果
     */
    @Override
    public int updateSysUserBase(SysUserBase sysUserBase) {
        if (super.updateById(sysUserBase)) {
            SysUserBase userBase = super.getById(sysUserBase.getId());
            boxSupplierSync(userBase, false);
            return 1;
        }
        return 0;
    }

    /**
     * 批量删除用户基本信息
     *
     * @param ids 需要删除的用户基本信息主键
     * @return 结果
     */
    @Override
    public int deleteSysUserBaseByIds(String ids) {
        String[] arrId = Convert.toStrArray(ids);
        for (String id : arrId) {
            SysUserBase sysUserBase = new SysUserBase();
            sysUserBase.setId(id);
            boxSupplierSync(sysUserBase, true);
        }
        return sysUserBaseMapper.deleteSysUserBaseByIds(arrId);
    }

    /**
     * 删除用户基本信息信息
     *
     * @param id 用户基本信息主键
     * @return 结果
     */
    @Override
    public int deleteSysUserBaseById(String id) {
        SysUserBase sysUserBase = new SysUserBase();
        sysUserBase.setId(id);
        boxSupplierSync(sysUserBase, true);
        return sysUserBaseMapper.deleteSysUserBaseById(id);
    }

    /**
     * 货箱生产商同步
     *
     * @param sysUserBase
     * @param isDel
     * @return void
     * @author yw
     * @date 2023-11-24 14:46:07
     */
    @Override
    public void boxSupplierSync(SysUserBase sysUserBase, boolean isDel) {
        if (StrUtil.isEmpty(sysUserBase.getUserId())) {
            throw new UtilException("用户信息为空");
        }
        log.error("货箱生产商同步参数：{}", JSON.toJSONString(sysUserBase));
        SysUserBase userBase = super.getById(sysUserBase.getId());
        if (isDel) {
            /**删除同步**/
            String url = DictUtils.getDictValue(Constants.INTER_TRANSMIT_PLATFORM_SYNC_INFO, Constants.BOX_SUPPLIER_DEL);
            String domain = DictUtils.getDictValue(Constants.PLATFORM_DOMAIN, Constants.PLATFORM_DOMAIN_INTER);
            if (StrUtil.hasEmpty(domain, url)) {
                throw new UtilException("平台接口配置为空");
            }
            Map<String, Object> map = new HashMap<>();
            map.put("id", userBase.getUserId());
            String content = JSON.toJSONString(map);
            String codeResult = HttpRequest.post(domain + url).body(content).execute().body();
            AjaxResult codeAjaxResult = JSONUtil.toBean(codeResult, AjaxResult.class);
            log.error("货箱生产商同步平台返回：" + JSONUtil.toJsonStr(codeAjaxResult));
            if (!String.valueOf(codeAjaxResult.get("isOk")).equals("1")) {
                log.error("删除异常：{}，{}，{}", domain + url, content, codeResult);
                throw new UtilException("删除异常:" + codeResult);
            }
        } else {
            /**新增或修改同步**/
            String url = DictUtils.getDictValue(Constants.INTER_TRANSMIT_PLATFORM_SYNC_INFO, Constants.BOX_SUPPLIER_SYNC);
            String domain = DictUtils.getDictValue(Constants.PLATFORM_DOMAIN, Constants.PLATFORM_DOMAIN_INTER);
            if (StrUtil.hasEmpty(domain, url)) {
                throw new UtilException("平台接口配置为空");
            }
            userBase.setName(sysUserBase.getConcatName());
            userBase.setId(userBase.getUserId());
            String content = JSON.toJSONString(userBase);
            String codeResult = HttpRequest.post(domain + url).body(content).execute().body();
            AjaxResult codeAjaxResult = JSONUtil.toBean(codeResult, AjaxResult.class);
            log.error("货箱生产商同步平台返回：" + JSONUtil.toJsonStr(codeAjaxResult));
            if (!String.valueOf(codeAjaxResult.get("isOk")).equals("1")) {
                log.error("新增或修改异常：{}，{}，{}", domain + url, content, codeResult);
                throw new UtilException("新增或修改异常:" + codeResult);
            }
        }
    }
}
